﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class MessageCenter : Singleton<MessageCenter>
{

    /// <summary>
    /// 消息modle
    /// </summary>
    class MsgHandle
    {
        public object receiver;
        public Action<object[]> callback;

        public MsgHandle(object receiver, Action<object[]> callback)
        {
            this.receiver = receiver;
            this.callback = callback;
        }
    }

    /// <summary>
    /// 消息modle集合
    /// </summary>
    static Map<string, List<MsgHandle>> mMsgHandlerDict = new Map<string, List<MsgHandle>>();


    /// <summary>
    /// 注册消息
    /// </summary>
    public void AddObserver(object observer, string msgName, Action<object[]> callback)
    {
        // 略过
        if (string.IsNullOrEmpty(msgName))
        {
            LogUtil.LogDebug("AddObserver:" + msgName + " is Null or Empty");
            return;
        }

        // 略过
        if (null == callback)
        {
            LogUtil.LogDebug("AddObserver:" + msgName + " callback is Null");
            return;
        }

        // 略过
        if (!mMsgHandlerDict.ContainsKey(msgName))
        {
            mMsgHandlerDict[msgName] = new List<MsgHandle>();
        }

        // 看下这里
        var handlers = mMsgHandlerDict[msgName];

        // 略过
        // 防止重复注册
        foreach (var handler in handlers)
        {
            if (handler.receiver == observer && handler.callback == callback)
            {
                LogUtil.LogDebug("AddObserver:" + msgName + " ayready Register");
                return;
            }
        }

        // 再看下这里
        handlers.Add(new MsgHandle(observer, callback));
    }

    public void AddObserver(object observer, Enum msgName, Action<object[]> callback)
    {
        AddObserver(observer, msgName.ToString(), callback);
    }

    /// <summary>
    /// 发送消息
    /// 注意第一个参数
    /// </summary>
    public static void SendMsg(string msgName, params object[] paramList)
    {
        if (string.IsNullOrEmpty(msgName))
        {
            LogUtil.LogDebug("SendMsg is Null or Empty");
            return;
        }

        if (!mMsgHandlerDict.ContainsKey(msgName))
        {
            LogUtil.LogDebug("SendMsg is UnRegister");
            return;
        }

        var handlers = mMsgHandlerDict[msgName];


        var handlerCount = handlers.Count;

        // 之所以是从后向前遍历,是因为  从前向后遍历删除后索引值会不断变化
        // 参考文章,http://www.2cto.com/kf/201312/266723.html
        for (int index = handlerCount - 1; index >= 0; index--)
        {
            var handler = handlers[index];

            if (handler.receiver != null)
            {
                LogUtil.LogDebug("SendLogicMsg:" + msgName + " Succeed");
                handler.callback(paramList);
            }
            else {
                handlers.Remove(handler);
            }
        }
    }

        // Use this for initialization
        void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
